/**
 * Created by zhanglihe on 15/11/10.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function( $, common, ready, Collection, template, Model, dialog ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'showcase',
                condition: {limit: 10, offset: 0},
                model: {
                    url:_global.url.api + 'showcase',
                    event: {
                        '.item-remove:click': 'remove',
                        '.edit_listorder:change': 'setlistorder'
                    },
                    remove: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            'content': '确定删除吗?',
                            ok: function() {
                                if(model.del()){
                                    _this.collection.reload();
                                };
                            }, cancel: function() {}
                        }).show(e.target);
                    },
                    setlistorder:function(e){
                        e.preventDefault();
                        var id = this.data.id;
                        var listorder = $('input[name="edit_listorder_' + id + '"]').val();
                        if(new Model({
                                url: _global.url.api + 'showcase',
                                data:{id:id,listorder:listorder,act:'order'}
                            }).put()) {
                            _this.collection.reload();
                        };
                    }

                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            var _this = this;
            $('#btn-search').on('click', function(e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });
            $("#btn-add").on("click",function(e){
                e.preventDefault();
                var html=template('goodsadd_tpl');
                dialog({
                    'content': html,
                    ok: function() {
                        var goods =new Array();
                        $("#reason_list").find("input[name='goods']").each(function(){
                            goods.push($(this).val());
                        });
                        var goods_id =goods.join(',');
                        if(goods_id==''){
                            Alert({msg:'请选择商品',type:'error'});
                            return false;
                        }
                        new Model({
                            url: _global.url.api + 'showcase',
                            data:{goods_id:goods_id}
                        }).post();
                        _this.collection.reload();
                    }, cancel: function() {}
                }).show();
                goods_list();
                $("#btn-searchs").on('click',function(e){
                    var keyword =$("#keywords").val();
                     goods_list(keyword);
                });
            });
        }


    };
var goods_list =function(keyword){
        this.collection = new Collection({
            container: 'data-list_goods',
            template: 'goods_check',
            url: _global.url.api + 'goodsnice_get',
            condition: {keyword:keyword},
            model: {
                event: {
                    '.item-select:click': 'goodsnice_set',
                    '.item-select_redy:click':'goodsnice_qx'
                },
                goodsnice_set: function(e) {
                    e.preventDefault();
                    var id =this.data.id;
                    var goodscount =this.data.showcasecount;
                    if(!check_goods(id,'showcase')){
                        return false;
                    };
                    var goods =new Array();
                    $("#reason_list").find("input[name='goods']").each(function(){
                        goods.push($(this).val());
                    });
                    var count =goods.length;
                    console.log(count);
                    if(count>=3||count+goodscount+1>3){
                        Alert({msg:'橱窗商品不能多于3个',type:'error'});
                        return false;
                    }
                    $(".item-selects"+id+"").show();
                    $(".item-select"+id+"").hide();
                    $("#reason_list").append('<input type="hidden" value="'+id+'" id="goods'+id+'" name="goods">');

                },
                goodsnice_qx: function(e) {
                    e.preventDefault();
                    var id =this.data.id;
                    $(".item-selects"+id+"").hide();
                    $(".item-select"+id+"").show();
                    $("#goods"+id).remove();
                }

            },
            callback: function() {
                loaded();
            }
        });
        this.collection.fetch();
    }
    var  check_goods=function(id,status){
        var flag =false;
        $.ajax({
            url: _global.url.api + 'goodsnice_check',
            type: 'get',
            async: false,
            data:{id:id,status:status},
            success: function(res) {
                if(res.code == 0) {
                    flag=true;
                }else{
                    Alert({msg:res.msg,type:'error'});
                    flag=false;
                }
            }
        });
        return flag;
    }

    new index();

});